package com.kabigon.tiny.test.bean;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.lang.Console;
import cn.hutool.json.JSONUtil;
import com.kabigon.tiny.test.bean.entity.Source;
import com.kabigon.tiny.test.bean.entity.Target;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.function.BiFunction;

/**
 *
 */
public class Main {

    public static void main(String[] args) {
        List<Source> sourceList = new ArrayList<>();
        sourceList.add(new Source("Alice", 25, "1"));
        sourceList.add(new Source("Bob", 30, "1"));

        HashMap<String, String> filedMapping = new HashMap<>(16);
        filedMapping.put("sourceName", "targetName");
        filedMapping.put("sourceAge", "targetAge");
        filedMapping.put("isValid", "delstatus");
        BiFunction<String, Object, Object> fieldValueEditor = (key, value) -> {
            if ("delstatus".equalsIgnoreCase(key)) {
                return "1".equals(value.toString()) ? 0 : 1;
            }

            return value;
        };
        List<Target> targetList = BeanUtil.copyToList(sourceList, Target.class, CopyOptions.create()
                .setFieldMapping(filedMapping)
                .setFieldValueEditor(fieldValueEditor)
        );
        Console.log("targetList:{}", JSONUtil.toJsonPrettyStr(targetList));

    }

}
